package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;


/**
 * Servlet implementation class ScoreServlet
 */
public class ScoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScoreServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("score controller called");
		request.setCharacterEncoding("utf8");
		response.setContentType("text/html; charset=UTF-8");
		
		Connection conn;
		ResultSet rs = null;
		
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		
		System.out.println("kind : "+request.getParameter("kind")+" code : "+request.getParameter("code")+" preid : "+request.getParameter("preid")
				+" down_flag : "+request.getParameter("down_flag")+" uid : "+session.getAttribute("id"));
		
		if(request.getParameter("kind").equals("ranking")){
			System.out.println("score return ranking");

			//상위 세명
			
			try {
				conn = process.DBConnectionPool.getInstance().getConnetion();
			
			
			String sql = "select * from user where score is not null order by score desc limit 3;";
			PreparedStatement st;
	
			List userlist = new ArrayList();
			st = conn.prepareStatement(sql);
			Gson gson = new Gson();

		    rs = st.executeQuery();
		    
		    Integer idx = 1;
		    
		    while(rs.next()){
				Map<String, String> item = new HashMap<String, String>();
				item.put("userid",rs.getString("id"));
				item.put("username",rs.getString("name"));
				item.put("rank",idx.toString());
				item.put("score",rs.getString("score"));
				idx++;
				userlist.add(item);
		    }
		    
		    
		    String json = gson.toJson(userlist);
			System.out.println("Info : "+json);
			out.println(request.getParameter("callback")+"("+json+")");
			
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
		}
		
	/*	
		
		//presenting update
			//surid 설문지 mapping (안할 수 도 잇고)
		else if(request.getParameter("kind").equals("update")){
			System.out.println("presenting-update.. come");
			String code = request.getParameter("code");
			String surId = request.getParameter("surid");
			if(surId!=null)
				sys.dao.presentingDAO.addSurvey(code, surId);
			
			out.println((String)session.getAttribute("id")+"&"+(String)session.getAttribute("name"));
			
		}
		
		//presenting delete
			//유저가 presenting 준비하던걸 취소
		else if(request.getParameter("kind").equals("cancel")){
			System.out.println("presenting - cancel.. come");
			String userId = (String)session.getAttribute("id");
			
			sys.dao.presentingDAO.deleteItems(userId);
				
			out.println("발표 준비가 취소 되었습니다.");
			
		}
		
		//presenting show
			//현재 진행중인 presenting list출력
		else if(request.getParameter("kind").equals("show")){
			System.out.println("presenting - show.. come");
			
			Gson gson = new Gson();
			
			List pList = sys.dao.presentingDAO.getPresentingList();
			
			String json = gson.toJson(pList);
			
			System.out.println("pList : "+json);
			
			out.println(request.getParameter("callback")+"("+json+")");
			
		}
		
		//해당 유저의 진행중인 발표 골라내기
		else if(request.getParameter("kind").equals("ing_check")){
			System.out.println("presenting - which presentation is presenting");
			
			String userId = (String)session.getAttribute("id");
			String docid="";
			
			docid = sys.dao.presentingDAO.getIngItemOfUser(userId);
		
			out.println(docid);
		}

		//해당 id의 문서 및 발표 정보 리턴
		else if(request.getParameter("kind").equals("get_info")){
			System.out.println("presenting - get info");
			
			Gson gson = new Gson();
			//세션에서 받아오면 안되지..
						
			String docid=request.getParameter("gid");
			String code=request.getParameter("code");
	
			String json = gson.toJson(sys.dao.presentingDAO.getInfoByCode(code));
			System.out.println("Info : "+json);
			out.println(request.getParameter("callback")+"("+json+")");
		}
		//out.println("haha");
	}
	*/
		
		
	}

}
